package com.medical.literature.user.controller;

import com.medical.literature.common.vo.Result;
import com.medical.literature.user.dto.LoginRequest;
import com.medical.literature.user.dto.RegisterRequest;
import com.medical.literature.user.dto.UserInfoResponse;
import com.medical.literature.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Enumeration;

/**
 * 用户认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final UserService userService;
    private final PasswordEncoder passwordEncoder;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@Valid @RequestBody RegisterRequest request) {
        userService.register(request);
        return Result.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody LoginRequest request) {
        String token = userService.login(request);
        return Result.success("登录成功", token);
    }

    /**
     * 测试BCrypt密码验证
     */
    @GetMapping("/test-bcrypt")
    public Result<String> testBCrypt() {
        String dbPassword = "$2b$10$2TAXfztRpHsu6A0iEMFObepK5LwnJmsBgx8YJ0eALQ9Biiic";
        String rawPassword = "123456";
        
        boolean matches = passwordEncoder.matches(rawPassword, dbPassword);
        log.info("数据库密码: {}", dbPassword);
        log.info("原始密码: {}", rawPassword);
        log.info("密码匹配结果: {}", matches);
        
        return Result.success("密码匹配结果: " + matches);
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/userinfo")
    public Result<UserInfoResponse> getUserInfo(@RequestHeader("X-User-Id") Long userId, HttpServletRequest httpRequest) {
        log.info("接收到获取用户信息请求，X-User-Id: {}", userId);
        
        // 打印所有请求头
        Enumeration<String> headerNames = httpRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            log.info("请求头 {}: {}", name, httpRequest.getHeader(name));
        }
        
        UserInfoResponse userInfo = userService.getUserInfo(userId);
        return Result.success(userInfo);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/userinfo")
    public Result<Void> updateUserInfo(@RequestHeader("X-User-Id") Long userId,
                                     @Valid @RequestBody UserInfoResponse userInfo) {
        userService.updateUserInfo(userId, userInfo);
        return Result.success();
    }
    
    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    public Result<Void> changePassword(@RequestHeader("X-User-Id") Long userId,
                                     @Valid @RequestBody ChangePasswordRequest request) {
        userService.changePassword(userId, request.getOldPassword(), request.getNewPassword());
        return Result.success();
    }
    
    /**
     * 修改密码请求DTO
     */
    public static class ChangePasswordRequest {
        private String oldPassword;
        private String newPassword;
        
        // Getters and setters
        public String getOldPassword() {
            return oldPassword;
        }
        
        public void setOldPassword(String oldPassword) {
            this.oldPassword = oldPassword;
        }
        
        public String getNewPassword() {
            return newPassword;
        }
        
        public void setNewPassword(String newPassword) {
            this.newPassword = newPassword;
        }
    }
}